//
//  WmcProgressBarView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/7/15.
//

import UIKit

class WmcProgressBarView: WmcBaseView {
    
    var cancelClosure: (() -> Void)?
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_white_FFFFFF
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        return view
    }()
    
    private lazy var closeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage("close".wmc_image, for: .normal)
        btn.imageEdgeInsets = UIEdgeInsets.init(top: 1, left: 1, bottom: 1, right: 1)
        return btn
    }()
    
    private lazy var progressBar: UIProgressView = {
        let view = UIProgressView(progressViewStyle: .default)
        view.progressTintColor = .wmc_blue_5290FD
        view.trackTintColor = .wmc_grayH19_F5F5F5
        view.layer.cornerRadius = 8
        return view
    }()
    
    private lazy var textLabel: UILabel = {
        let label = UILabel()
        label.font = 12.wmc_font
        label.textColor = .wmc_grayH8_8A8A99
        label.textAlignment = .center
        return label
    }()
    
    @discardableResult
    static func showInView(_ view: UIView) -> WmcProgressBarView {
        let progressBarView = WmcProgressBarView()
        progressBarView.backgroundColor = .wmc_grayH2_323233.withAlphaComponent(0.7)
        view.addSubview(progressBarView)
        
        progressBarView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
        }
        
        return progressBarView
    }
    
    func setProgress(_ progressValue: Float, progressText: String) {
        progressBar.setProgress(progressValue, animated: true)
        textLabel.text = progressText
    }
    
    func dismiss() {
        removeFromSuperview()
    }
    
    override func makeUI() {
        addSubview(bgView)
        bgView.addSubview(closeBtn)
        bgView.addSubview(progressBar)
        bgView.addSubview(textLabel)
        
        bgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.left.equalTo(28)
            make.right.equalTo(-28)
            make.height.equalTo(98)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.right.equalTo(-8)
            make.width.height.equalTo(18)
        }
        
        progressBar.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(6)
            make.top.equalTo(42)
        }
        
        textLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(progressBar.snp.bottom).offset(10)
        }
    }
    
    override func bindViewAction() {
        closeBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.cancelClosure?()
            weakSelf.removeFromSuperview()
        }
    }
}
